<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="cancel">
    <el-form ref="addEmployee" :model="formData" :rules="rules" label-width="150px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width: 50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width: 50%" placeholder="请输入手机" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width: 50%" placeholder="请输入部门" @focus="getDepartments" />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          :props="{ label: 'name' }"
          :default-expand-all="true"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width: 50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" type="date" style="width: 50%" placeholder="请输入入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width: 50%" placeholder="请输入聘用形式">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" type="date" style="width: 50%" placeholder="请输入转正时间" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
import { getRequiredRules } from '@/utils/validate'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [
          getRequiredRules('用户姓名'),
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          getRequiredRules('手机号'),
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        timeOfEntry: [getRequiredRules('入职时间')],
        formOfEmployment: [getRequiredRules('聘用形式')],
        workNumber: [getRequiredRules('工号')],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }]
        // 在选择树形结构时会触发blur，因此这里使用change触发校验
      },
      treeData: [],
      showTree: false,
      loading: false,
      EmployeeEnum
    }
  },
  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    // 点击选择树型节点
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    async submit() {
      try {
        await this.$refs.addEmployee.validate()
        await addEmployee(this.formData)
        // $parent父组件实例，子组件的父级必须要是原生DOM标签
        this.$parent.getEmployeeList()
        this.$parent.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    cancel() {
      this.formData = {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      }
      this.$refs.addEmployee.resetFields()
      // this.$parent.showDialog = false
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style scoped>
.el-tree {
  position: absolute;
  z-index: 1;
  width: 300px;
  height: 300px;
  overflow: auto;
  border: 1px solid #ddd;
  border-radius: 8px;
}
</style>
